package com.wc.AlgoOJ_train.train5_数据结构.c6_并查集.DS0903_行进路线;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/25 22:51
 * @description http://43.138.190.70:8888/p/DS0903
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    static long[][] a = new long[N][3];
    static int[] h = new int[N];
    static int t, ex, ey, n;

    public static void main(String[] args) {
        t = sc.nextInt();
        while (t-- > 0) {
            ex = sc.nextInt();
            ey = sc.nextInt();
            n = sc.nextInt();
            for (int i = 0; i <= n + 1; i++) h[i] = i;
            for (int i = 1; i <= n; i++) {
                a[i][0] = sc.nextInt();
                a[i][1] = sc.nextInt();
                a[i][2] = sc.nextInt();
            }
            // 和起点连接
            for (int i = 1; i <= n; i++) {
                long x = a[i][0];
                long y = a[i][1];
                long r = a[i][2] + 1;
                if (x * x + y * y <= r * r) {
                    merge(0, i);
                }
            }
            // 边互相连接
            for (int i = 1; i <= n; i++) {
                for (int j = i + 1; j <= n; j++) {
                    long x = a[i][0] - a[j][0];
                    long y = a[i][1] - a[j][1];
                    long r = a[i][2] + a[j][2];
                    if (x * x + y * y <= r * r) {
                        merge(i, j);
                    }
                }
            }
            // 与终点相连
            for (int i = 1; i <= n; i++) {
                long x = ex - a[i][0];
                long y = ey - a[i][1];
                long r = a[i][2];
                if (x * x + y * y <= r * r) {
                    merge(i, n + 1);
                }
            }
            if (find(0) == find(n + 1)) {
                out.println(1);
            } else {
                out.println(0);
            }
        }
        out.flush();
    }

    static void merge(int x, int y) {
        h[find(x)] = find(y);
    }

    static int find(int x) {
        if (h[x] != x) h[x] = find(h[x]);
        return h[x];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
